package cn.pugle.ormtest.mybatis;

import cn.pugle.ormtest.mybatis.bean.CustomerBean;
import cn.pugle.ormtest.mybatis.dao.CustomerMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static cn.pugle.ormtest.common.JSON.SNAKE_MAPPER;

/**
 * 需要pom中把xml文件作为资源
 * @author tzp
 * @date 2017/12/22
 */
@Service
public class MybatisDemo {
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private SqlSession sqlSesssion;

    public void retrieve() throws JsonProcessingException {
        //retrieve for one with annotation
        CustomerBean oneCustomer = customerMapper.selectOne(1L);
        System.out.println("oneCustomer: " + SNAKE_MAPPER.writeValueAsString(oneCustomer));

        //retrieve for one with xml
        CustomerBean oneCustomer1 = customerMapper.selectOne1(1L);
        System.out.println("oneCustomer1: " + SNAKE_MAPPER.writeValueAsString(oneCustomer1));

        //use mybatis sqlsession
        CustomerBean oneCustomer2 = sqlSesssion.selectOne("selectOne2", 1L);
        System.out.println(SNAKE_MAPPER.writeValueAsString(oneCustomer2));
    }
    //比较简单纯粹, create / update / delete 就不用试了


}
